var id = T.p("id");


$(function () {
    $('#poolOptions-pg').propertygrid({
        // data:rows,
        scrollbarSize: 0,
        height: 200,
        columns: [[
            {field: 'name', title: '配置项', width: 200, sortable: true},
            {field: 'value', title: '配置值', width: 100, resizable: false}
        ]]
    });

});
var dbTypes = T.getDictItems("db_type");
var poolTypes = T.getDictItems("pool_type");
var remark = function (arr, index, key) {
    var remark = eval('(' + arr[index].remark + ')');
    return remark[key];
};

var initPoolOptions = function (options) {
    setTimeout(function () {
        var poolOptions = EasyUIUtils.toPropertygridRows(options);
        $('#poolOptions-pg').propertygrid('loadData', poolOptions);
    }, 100)
};

var vm = new Vue({
    el: '#rrapp',
    data: {
        title: "新增",
        workDb: {
            dbType: dbTypes[0].dictCode,
            driverClass: remark(dbTypes, 0, "driverClass"),
            dbUrl: remark(dbTypes, 0, "jdbcUrl"),
            poolType: poolTypes[0].dictCode
        },
        dbTypes: dbTypes,
        poolTypes: poolTypes
    },
    created: function () {
        if (id != null) {
            this.title = "修改";
            this.getInfo(id)
        } else {
            initPoolOptions(remark(poolTypes, 0, "options"));
        }
    },
    methods: {
        getInfo: function (id) {
            $.get("../workDb/info/" + id, function (r) {
                vm.workDb = r.workDb;
                setTimeout(function () {
                    var data = EasyUIUtils.toPropertygridRows(eval('(' + vm.workDb.poolOptions + ')'));
                    $('#poolOptions-pg').propertygrid('loadData', data)
                }, 100);

            });
        },
        saveOrUpdate: function (event) {
            var rows = $('#poolOptions-pg').propertygrid('getRows');
            var poolOptions = JSON.stringify(EasyUIUtils.toPropertygridMap(rows))
            vm.workDb.poolOptions = poolOptions;
            var url = vm.workDb.id == null ? "../workDb/save" : "../workDb/update";
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.workDb),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.back();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        back: function (event) {
            history.go(-1);
        },
        changeDbType: function (event) {
            var selectValue = event.target.value;
            dbTypes.forEach(function (item) {
                if (selectValue == item.dictCode) {
                    vm.workDb.dbType = item.dictCode;
                    var remark = eval('(' + item.remark + ')');
                    vm.workDb.driverClass = remark["driverClass"];
                    vm.workDb.dbUrl = remark["jdbcUrl"];
                }
            });
        },
        changePoolType: function (event) {
            var selectValue = event.target.value;
            poolTypes.forEach(function (item) {
                if (selectValue == item.dictCode) {
                    vm.workDb.poolType = item.dictCode;
                    var remark = eval('(' + item.remark + ')');
                    initPoolOptions(remark["options"]);
                }
            });
        },
        testConnection: function (event) {
            $.get("/workDb/testConnection", {
                "url": vm.workDb.dbUrl,
                "user": vm.workDb.dbUser,
                "pass": vm.workDb.dbPassword
            }, function (r) {
                if (r.code == 0 && r.resData == 1) {
                    alert("连接成功");
                } else {
                    alert("连接失败");
                }
            });
        }
    }
});

